﻿(function ($) {

    var calculateLayout = function () {
        $('#nineseas').height($('body').height() - 40 - 50 - 15);
        
        var contentHeight = $('#nineseas').height() - 40 - 40 - 24;
        $('#nineseas-reader-content').height(contentHeight);

        var contentEvents = $('#nineseas-reader-content').data("events");

        $('#nineseas-reader-content').trigger('heightChanged', contentHeight);
        $('body').css('font-size', '100%');

        var content = $('#content-area');
        if (content.length > 0) {
            content.height(contentHeight - 24);
        }
    };

    $(window).resize(function () {
        calculateLayout();
    });
    $.debugOptions = {
        enabled: false
    };
    $.debugManager = {
        _isInit: false,
        init: function () {
            if (!$.debugOptions.enabled || this._isInit) return;

            $("<div id='debug-container' style='display: none;'></div>").appendTo("body");

            var container = $("#debug-container");

            container.ajaxError(function (event, request, settings) {
                container.dialog({
                    title: "Error requesting page " + settings ? settings.url : "",
                    resizable: false,
                    modal: true,
                    height: 600,
                    width: 800
                });
                container.html('<iframe src="about:blank" width="100%" height="100%" frameborder="1" scrolling="auto"></iframe>');
                $(request.responseText).appendTo(container.find("iframe").contents().find('body'));
            });

            this._isInit = true;
        }
    };

    $.debug = function () {
        if (!$.debugOptions.enabled) return;
    };
    $(document).ready(function () {
        $('#throbber')
            .hide()
            .ajaxStart(function () {
                $(this).show();
            })
            .ajaxStop(function () {
                $(this).hide();
            });

        $.debugManager.init();
        calculateLayout();
        window.setTimeout(calculateLayout, 50);
    });

})(jQuery);